package ru.usu.gv.gui.component;

import ru.usu.gv.core.ResourceLoader;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 @author Sergey Pupyrev 
 19.05.2007
 */

public class GVDropDownButtonPanel extends JPanel
{
	private JButton actionButton;
	private JButton typeButton = new JButton();
	private JPopupMenu typeMenu;
	private GridBagLayout layout = new GridBagLayout();
	private IDropDownItem currentItem = null;

	public GVDropDownButtonPanel(Action action, List<IDropDownItem> items, IDropDownItem activeItem)
	{
		currentItem = activeItem;
		initTypeMenu(items);
		setLayout(layout);
		actionButton = new JButton(action);
		actionButton.setMinimumSize(new Dimension(35, 28));
		actionButton.setMaximumSize(new Dimension(35, 28));
		actionButton.setPreferredSize(new Dimension(35, 28));
		actionButton.setBorder(null);
		actionButton.setText("");
		if (currentItem.getIcon() != null)
			actionButton.setIcon(currentItem.getIcon());
		else
			actionButton.setText(currentItem.getTitle());
		typeButton.setBorder(null);
		typeButton.setMinimumSize(new Dimension(28, 28));
		typeButton.setMaximumSize(new Dimension(28, 28));
		typeButton.setPreferredSize(new Dimension(28, 28));
		typeButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				typeMenu.show(typeButton, typeButton.getX() - 84, typeButton.getY());
			}
		});
		this.setPreferredSize(new Dimension(45, 32));
		this.setMaximumSize(new Dimension(70, 32));
		typeButton.setIcon(ResourceLoader.loadImage("images/dropArrow.png"));
		this.add(actionButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.RELATIVE, new Insets(0, 0, 0, 0), 0, 0));
		this.add(typeButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.REMAINDER, new Insets(0, 0, 0, 0), 0, 0));
	}

	@Override
	public void setToolTipText(String text)
	{
		actionButton.setToolTipText(text);
		typeButton.setToolTipText(text);
		super.setToolTipText(text);
	}

	private void initTypeMenu(final List<IDropDownItem> items)
	{
		typeMenu = new JPopupMenu();
		typeMenu.removeAll();
		for (final IDropDownItem item : items)
		{
			if (currentItem == null)
				currentItem = item;

			JMenuItem typeMenuItem = new JMenuItem(item.getTitle());
			typeMenuItem.setIcon(item.getIcon());

			typeMenuItem.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					currentItem = item;
					Icon icon = item.getIcon();
					if (icon != null)
						actionButton.setIcon(icon);
					else
						actionButton.setText(item.getTitle());
				}
			});
			typeMenu.add(typeMenuItem);
		}
	}

	public IDropDownItem getActiveItem()
	{
		return currentItem;
	}
}
